home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP06 / CHECKER1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  3.9 KB  |  114 lines

  1. /*-------------------------------------------------
  2.    CHECKER1.C -- Mouse Hit-Test Demo Program No. 1
  3.                  (c) Charles Petzold, 1996
  4.   -------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define DIVISIONS 5
  9. #define MoveTo(hdc, x, y) MoveToEx (hdc, x, y, NULL)
  10.  
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR  szCmdLine, int iCmdShow)
  15.      {
  16.      static char szAppName[] = "Checker1" ;
  17.      HWND        hwnd ;
  18.      MSG         msg ;
  19.      WNDCLASSEX  wndclass ;
  20.  
  21.      wndclass.cbSize        = sizeof (wndclass) ;
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  33.  
  34.      RegisterClassEx (&wndclass) ;
  35.  
  36.      hwnd = CreateWindow (szAppName, "Checker1 Mouse Hit-Test Demo",
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.  
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.  
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.           {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.           }
  50.      return msg.wParam ;
  51.      }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  54.      {
  55.      static BOOL fState[DIVISIONS][DIVISIONS] ;
  56.      static int  cxBlock, cyBlock ;
  57.      HDC         hdc ;
  58.      PAINTSTRUCT ps ;
  59.      RECT        rect ;
  60.      int         x, y ;
  61.  
  62.      switch (iMsg)
  63.           {
  64.           case WM_SIZE :
  65.                cxBlock = LOWORD (lParam) / DIVISIONS ;
  66.                cyBlock = HIWORD (lParam) / DIVISIONS ;
  67.                return 0 ;
  68.  
  69.           case WM_LBUTTONDOWN :
  70.                x = LOWORD (lParam) / cxBlock ;
  71.                y = HIWORD (lParam) / cyBlock ;
  72.  
  73.                if (x < DIVISIONS && y < DIVISIONS)
  74.                     {
  75.                     fState [x][y] ^= 1 ;
  76.  
  77.                     rect.left   = x * cxBlock ;
  78.                     rect.top    = y * cyBlock ;
  79.                     rect.right  = (x + 1) * cxBlock ;
  80.                     rect.bottom = (y + 1) * cyBlock ;
  81.  
  82.                     InvalidateRect (hwnd, &rect, FALSE) ;
  83.                     }
  84.                else
  85.                     MessageBeep (0) ;
  86.                return 0 ;
  87.  
  88.           case WM_PAINT :
  89.                hdc = BeginPaint (hwnd, &ps) ;
  90.  
  91.                for (x = 0 ; x < DIVISIONS ; x++)
  92.                     for (y = 0 ; y < DIVISIONS ; y++)
  93.                          {
  94.                          Rectangle (hdc, x * cxBlock, y * cyBlock,
  95.                                    (x + 1) * cxBlock, (y + 1) * cyBlock) ;
  96.  
  97.                          if (fState [x][y])
  98.                               {
  99.                               MoveTo (hdc,  x    * cxBlock,  y    * cyBlock) ;
  100.                               LineTo (hdc, (x+1) * cxBlock, (y+1) * cyBlock) ;
  101.                               MoveTo (hdc,  x    * cxBlock, (y+1) * cyBlock) ;
  102.                               LineTo (hdc, (x+1) * cxBlock,  y    * cyBlock) ;
  103.                               }
  104.                          }
  105.                EndPaint (hwnd, &ps) ;
  106.                return 0 ;
  107.  
  108.           case WM_DESTROY :
  109.                PostQuitMessage (0) ;
  110.                return 0 ;
  111.           }
  112.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  113.      }
  114.